package tt.dz.appaction.base;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

/**
 * 基础控制器，存放通用控制器方法
 * @author Xielf
 *
 */
public class BaseController {

	/**
	 * JSON响应
	 * @param object 响应对象
	 * @param response
	 */
	public void outJson(Object object, HttpServletResponse response){
		
		PrintWriter out = null;
		try {
			String str = JSONObject.toJSONString(object);
			//设置响应头
			response.setContentType("application/json;UTF-8");
			out = response.getWriter();
			out.print(str);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				out.close();
			}
		}
	}
	
	/**
	 * 
	 *字符串响应
	 * @param str 响应字符串
	 * @param response
	 */
	public void outText(String str, HttpServletResponse response){
		
		PrintWriter out = null;
		try {
			//设置响应头
			response.setContentType("application/json;UTF-8");
			out = response.getWriter();
			out.print(str);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				out.close();
			}
		}
	}
}
